package com.ray.job;

import com.ray.bean.MachineBean;
import com.ray.bean.TemporalIntervalBean;
import com.ray.mapper.MachineMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MachineJob {

	@Autowired
	private MachineMapper machineMapper;
	

	@Scheduled(cron = "30 1-59 * * * ?")
	public void updateSwitchByTimeAndHandOperation() {
		List<MachineBean> machineBeans = machineMapper.getAllMachineBean();
		boolean shouldSetON = false;
		for (MachineBean machineBean : machineBeans) {
			if (!machineBean.isHandOperationing()) {
				for (TemporalIntervalBean temporalIntervalBean : machineBean
						.getWorkTemporalIntervals()) {
					if (temporalIntervalBean.checkNowInTemporalInterval()) {
						shouldSetON = true;
					}
				}
				if (shouldSetON) {
					if (!machineBean.switchIsOpen()) {
						machineBean.openMachine();
						machineMapper.updateMachine(machineBean);
					}
				} else {
					if (machineBean.switchIsOpen()) {
						machineBean.closeMachine();
						machineMapper.updateMachine(machineBean);
					}
				}
			}
		}
	}

	@Scheduled(cron = "30 1-59 * * * ?")
	public void updateSwitchByStandbyMachine() {
		List<MachineBean> machineBeans = machineMapper.getAllMachineBean();
		for (MachineBean machineBean : machineBeans) {
			if (machineBean.isStandby()) {
				System.err.println(machineBean.getTimeoutBreakElectricityTime());
				System.err.println(machineBean.getTimeoutBreakFlowTime());
			}
		}
	}
}
